<template>
  <div id="Shopping">
    <div class="bg"></div>
    <div class="sarch">
      <van-search placeholder="搜索喜欢的商品" v-model="value" @search="onSearch" />
    </div>
    <van-button size="large" square type="primary" class="mybtn" @click="load" v-if="myload">重新加载</van-button>
    <van-tabs v-model="active" sticky @click="onChangeTab" >
        <van-tab v-for="(item,k) in nav" :key="k" :title=item.name>
          <van-list
            v-model="loading"
            :finished="finished"
            @load="onLoad"
            :offset="100"
          >
            <div class="list">
              <div  v-for="(item,k) in list" :key="k" @click="$router.push({path:'/goods'})" class="good-list">
                <img :src="item.commodityPicture"/>
                <div>
                  <span>{{item.commodityName}}</span>
                  <div><span>￥{{item.commodityPrice }}</span><span>原价 <del>￥17.90</del></span></div>
                  <div class="lq">
                    <div><img src="/static/img/lq.png"/><span>领券  7元</span></div>
                    <span>已售{{item.sales}}万</span>
                  </div>
                  <div class="yg">
                    <div>预估赚￥5.20</div>
                    <div>升级赚￥5.20</div>
                  </div>
                </div>
              </div>
            </div>
            <div class="error" style="background-color:red;color:white;padding: 0.2rem" @click="reload" v-if="isReload">加载失败！点击重新加载</div>
          </van-list>
        </van-tab>
    </van-tabs>

  </div>

</template>

<script>
  export default {
    name: "coupon",
    data() {
      return {
        active: 0,
        value:'',
        list: [],
        nav:[],
        loading: false,
        finished: false,
        pageNum:1,
        pageSize:10,
        index:0,
        isReload:false,
        myload:false,
      };
    },
    methods: {
      onSearch() {
        var $this=this;
        alert($this.value)
      },
      onLoad() {
        // 异步更新数据
        this.loading=true;
        console.log("开始加载!");
        setTimeout(()=>{
          // console.log(this.nav);
          this.getListById(this.nav[this.active].id,this.pageNum,this.pageSize);
          this.pageNum++;
        },500)
      },
      findComType(){
        this.$POST('/commoditytype/findComType').then(res =>{
          if(res.meta.code==200){
            if(res.data.length>0){
              this.nav=res.data;
            }
          }
        }).catch(res=>{
          this.myload=true;
          console.log("获取首次数据失败!")
        })
      },
      onChangeTab(index,title){
        this.list=[];
        this.pageNum=1;
        this.finished=false;
      },
      getListById(id,pageNum,pageSize){
        var params = new URLSearchParams();
        params.append('pageSize', pageSize);       //你要传给后台的参数值 key/value
        params.append('pageNum', pageNum);
        params.append('id', id);
        this.$POST('/commodity/findCommByType',params,false).then(res =>{
          console.log(res);
          if(res.meta.code===200){
            if(res.data.length>0){
              this.list=this.list.concat(res.data);
              if(res.data.length<this.pageSize){
                this.finished=true;
                this.$toast("到底了亲！");
              }
            }else{
              this.finished=true;
              this.$toast("到底了亲!");
            }
          }
          this.loading=false;
          console.log("加载数据成功!");
        }).catch(res=>{
          this.loading=false;
          this.finished=true;
          this.pageNum--;
          this.isReload=true;
          console.log("获取数据失败!");
        })
      },
      reload(){
        this.finished=false;
        this.isReload=false;
      },
      load(){
        this.findComType();
        this.myload=false;
      }
    },
    created() {
      this.findComType();
    }

  }
</script>

<style scoped>
  .count{
    width: 100%;
    float: left;
    margin-bottom:60px;
  }
  .count>div{
    width: 100%;
    background: #ffffff;
    padding-top:10px;
    float: left;
  }
  .count>div>p{
    text-align:left;
    margin: 0;
    margin-left: 2%;
    margin-bottom:10px;
    padding-left: 10px;
    border-left: 3px solid #fe513f;
    color: #fe513f;
  }
  .details{
    background: #eeeeee;
    width:49%;
    float: left;
    margin-left:0.5%;
    margin-top:6px;
  }
  .details>div{
    width:100%;
    display: inline-block;
  }
  .details>div>img{
    display: block;
    width: 100%;
    border-top-left-radius:10px;
    border-top-right-radius:10px;
  }
  .details>div:last-child{
    background: #ffffff;
    position: relative;
    top: -5px;
    text-align: left;
    border-bottom-left-radius:10px;
    border-bottom-right-radius:10px;
    padding-bottom:5px;
  }
  .details>div:last-child>span{
    display: block;
    width: 95%;
    margin: 0 auto;
    font-size: 16px;
  }
  .details>div:last-child>div{
    margin-top: 15px;
  }
  .details>div:last-child>div>span:first-child{
    font-size: 22px;
    color: #fe513f;
  }
  .details>div:last-child>div>span:nth-child(2){
    font-size:14px;
    color: #959595;
    margin-left: 3px;
  }
  .details>div:last-child>div>span:last-child{
    font-size:14px;
    color: #fe513f;
    display: inline-block;
    padding: 1px 3px;
    border: 1px solid #fe513f;
    border-radius:10px;
    position: relative;
    right:4px;
    top: 3px;
    float: right;
  }
  .good-list{
    width: 95%;
    text-align: left;
    margin: 0 auto;
    margin-bottom: 10px;
  }
  .good-list:after{
    clear:both;
    content:'';
    display:block;
    width:0;
    height:0;
    visibility:hidden;
  }
  .list:after{
    clear:both;
    content:'';
    display:block;
    width:0;
    height:0;
    visibility:hidden;
  }
  .good-list>img{
    float: left;
    width: 50%;
  }
  .good-list>div{
    width: 48%;
    float: left;
    text-align:left;
    margin-left:2%;
  }
  .good-list>div>span{
    font-family: 黑体;
    font-size: 18px;
    font-weight: bold;
  }
  .good-list>div>div>span:first-child {
    font-size: 20px;
    color:#e12532;
  }
  .good-list>div>div>span:last-child{
    font-size: 13px;
    float:right;
    line-height: 26px;
    color: #a8a9a9;
  }
  .good-list>div>div{
    margin-top: 15px;
  }
  .good-list>div>div:after{
    content:"";
    display:block;
    height:0;
    clear:both;
    visibility:hidden;
  }
  .lq{
    width: 100%;
  }
  .lq:after{
    clear:both;
    content:'';
    display:block;
    width:0;
    height:0;
    visibility:hidden;
  }
  .lq>div{
    width: 40%;
    position: relative;
  }
  .lq>div:first-child{
    float: left;
    height:30px;
  }
  .lq>div>img{
    position: absolute;
    top: 0;
    left: 0;
    z-index: 0;
    display: block;
    width: 100%;
  }
  .lq>div>span{
    z-index: 1;
    text-align: right;
    line-height:25px;
    position: absolute;
    right:5px;
    color: #ffffff;
    font-size: 14px;
  }
  .yg{
    width: 100%;
    font-size: 12px;
  }
  .yg>div:first-child{
    float: left;
    padding: 2px 5px;
    background: #ffdcdc;
    color: #e12532;
    border-radius:7px;
  }
  .yg>div:last-child{
    float: right;
    padding: 2px 5px;
    background: linear-gradient(to right,#ff722d, #ff4b26 , #ff2820); /* 标准的语法 */
    border-radius:7px;
    color: #ffffff;
  }
  .list{
    margin-top: 10px;
  }
</style>
<style>
  body{
    background: #ffffff;
  }
  #Shopping .van-search{
    padding: 15px!important;
    background: #ffffff!important;
  }
  #Shopping .van-search .van-cell{
    border-radius: 25px;
    background: #f7f7f7;
    padding:3px 10px;
  }
  #Shopping .van-swipe{
    width: 100%;
  }
  #Shopping .van-swipe-item>img{
    display: block;
    width: 100%;
    height: 100%;
  }
  .van-notice-bar{
    background: #ffffff;
    color: #fe513f;
  }
  .van-notice-bar__wrap{
    margin-left:10px;
  }
  .bg{
    position: absolute;
    width: 100%;
    height: 100%;
    background:#ffffff;
    z-index: -2
  }
  #Shopping .van-tabs__wrap--scrollable .van-tab{
    flex:1;
  }
</style>
